package com.game.center.controller;

import com.game.center.common.ServerResponse;
import com.game.center.messages.request.LoginCodeRequestParam;
import com.game.center.messages.request.LoginRequestParam;
import com.game.center.messages.response.PlayerLoginResponseVo;
import com.game.center.service.LoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Tag(name = "02 用户管理", description = "用户管理 API")
@Slf4j
@RestController
@RequestMapping("/user")
@ApiResponses(@ApiResponse(responseCode = "200", description = "接口请求成功"))
public class LoginController {

    @Autowired
    protected LoginService loginService;

    @Operation(summary = "login", description = "账户密码登录接口")
    @PostMapping("/login")
    @ResponseBody
    public ServerResponse<PlayerLoginResponseVo> login(@Validated @RequestBody LoginRequestParam loginRequestParam, HttpServletRequest request) {
        PlayerLoginResponseVo playerLoginResponseVo = loginService.login(loginRequestParam);
        return new ServerResponse<PlayerLoginResponseVo>(playerLoginResponseVo);
    }

    @Operation(summary = "login_code", description = "短信验证码登录接口")
    @PostMapping("/login_code")
    @ResponseBody
    public ServerResponse<PlayerLoginResponseVo> loginCode(@Validated @RequestBody LoginCodeRequestParam loginParam, HttpServletRequest request) {
        PlayerLoginResponseVo playerLoginResponseVo = loginService.loginCode(loginParam);
        return new ServerResponse<PlayerLoginResponseVo>(playerLoginResponseVo);
    }

//    @Operation(summary = "register", description = "用户注册接口")
//    @PostMapping("register")
//    @ResponseBody
//    public ServerResponse<PlayerLoginResponseVo> register(@Validated @RequestBody RegisterRequestParam registerParam, HttpServletRequest request) {
//        String loginIp = request.getRemoteAddr();
//        PlayerLoginResponseVo voPlayer = loginService.register(registerParam, loginIp);
//        return new ServerResponse<PlayerLoginResponseVo>(voPlayer);
//    }

//    @Operation(summary = "logout", description = "退出登录接口")
//    @GetMapping("/logout")
//    @ResponseBody
//    public ServerResponse logout(HttpServletRequest request) {
//        String userToken = request.getHeader(JWTUtil.USER_TOKEN);
//        try {
//            AccountToken tokenContent = JWTUtil.getTokenContent(userToken, AccountToken.class);
//            long userId = tokenContent.getUserId();
//            loginService.logout(userId);
//        } catch (TokenException e){
//            return new ServerResponse<HttpStatus>(HttpStatus.UNAUTHORIZED);
//        }
//        return new ServerResponse<HttpStatus>(HttpStatus.OK);
//    }
}
